package com.ikangtai.shecare.main;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.baseView.ProgressWebView;
import com.ikangtai.shecare.common.baseView.ScrollInterface;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.InternetUtil;
import com.ikangtai.shecare.curve.OnChartClickListener;
import com.jaeger.library.StatusBarUtil;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;


/**
 * A simple {@link Fragment} subclass.
 */
public class KnowledgeFragment extends Fragment {
    private ProgressWebView webview;
    private boolean isWebViewScrolledToTop = true;
    private ViewStub viewStub;
    private Button refresh_btn;
    private PtrClassicFrameLayout ptrFrame;

    public KnowledgeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        long t1 = System.currentTimeMillis();
        AppLog.i("get in KnowledgeFragment start!");

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_knowledge, container, false);
        initView(view);
        showKnowledgeContent(view);

        long t2 = System.currentTimeMillis();
        AppLog.i("get in KnowledgeFragment end! time = " + (t2 - t1));
        return view;
    }

    private final void initView(final View view) {
        ptrFrame = (PtrClassicFrameLayout) view.findViewById(R.id.store_house_ptr_frame);
        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

                if (InternetUtil.hasInternet())
                    showKnowledgeContent(view);
                else {
                    Toast.makeText(view.getContext(), R.string.network_anomalies, Toast.LENGTH_SHORT).show();
                    frame.refreshComplete();
                }
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                if (isWebViewScrolledToTop)
                    return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                else
                    return false;
            }
        });

        webview = (ProgressWebView) view.findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                ptrFrame.refreshComplete();
            }
        });

        webview.addJavascriptInterface(new OnChartClickListener(getActivity()), "KnowledgeClickListener");

        viewStub = (ViewStub) view.findViewById(R.id.viewStub);
    }

    private void showKnowledgeContent(View view) {
        if (InternetUtil.hasInternet())
            showWebKnowledge();
        else
            showBadNetwork(view);
    }

    private void showWebKnowledge() {
        viewStub.setVisibility(View.GONE);
        webview.setVisibility(View.VISIBLE);

//        webview.loadUrl("http://ikangtai.com/knowledgeService/knowledgeWZZ.html");
        webview.loadUrl(App.fragmentKnowledgeUrlPath);

        webview.setOnCustomScroolChangeListener(new ScrollInterface() {
            @Override
            public void onSChanged(int l, int t, int oldl, int oldt) {
                AppLog.i("webview.getScrollY() = " + webview.getScrollY());
                //已经处于顶端
                if (webview.getScrollY() == 0) {
                    isWebViewScrolledToTop = true;
                } else
                    isWebViewScrolledToTop = false;
            }

        });
    }

    private void showBadNetwork(View view) {
        webview.setVisibility(View.GONE);
        viewStub.setVisibility(View.VISIBLE);
        refresh_btn = (Button) view.findViewById(R.id.refresh_btn);

        refresh_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (InternetUtil.hasInternet())
                    showWebKnowledge();
            }
        });
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.orange), 0);
        }
    }

}
